<?php
/**
 ***********************************************************************************************************************
 * @author llq  2015/5/14  15:34
 ***********************************************************************************************************************
 */
namespace soa\express;
use soa\client;

/**
 * Class shop
 */
class shop extends client
{
    /**
     * 服务地址
     * @var string
     */
    public $service_url = 'http://express.core.kuaidihelp.com/';

    /**
     * 服务名称
     * @var string
     */
    protected $service_name = 'kuaidihelp_express';


    /**
     * 根据index_shop_id获取网点信息
     * @param $sid
     * @return bool|null
     */
    public function getShopById($sid)
    {
        if(empty($sid) || !is_numeric($sid)) {
            return null;
        }
        return $this->get('v1/shop/' . $sid);
    }

    /**
     * 根据home_shop_id获取网点信息
     * @param $sid
     * @return bool|null
     */
    public function getShopByHomeShopId($sid)
    {
        if(empty($sid) || !is_numeric($sid)) {
            return null;
        }
        return $this->get('v1/shop/' . $sid . '?field=home_shop_id');
    }


    /**
     * 根据nohome_shop_id获取网点信息
     * @param $sid
     * @return bool|null
     */
    public function getShopByNoHomeShopId($sid)
    {
        if(empty($sid) || !is_numeric($sid)) {
            return null;
        }
        return $this->get('v1/shop/' . $sid . '?field=nohome_shop_id');
    }

    /**
     * 根据品牌和名称获取网点信息
     * @param $name
     * @param $brand
     * @return mixed|null
     */
    public function getShopByNameBrand($name, $brand)
    {
        if(empty($name) || !is_string($name) || empty($brand) || !is_string($brand)) {
            return null;
        }
        $ret = $this->post('v1/shop/find', array('name' => $name, 'brand' => $brand, 'page_num' => 1));
        if($ret) {
            return current($ret);
        }
        return null;
    }

    /**
     * 批量获取网点数据列表
     * @param array $where
     * @param array $limit
     * @param string $order_by
     * @return bool
     */
    public function getShops($where = array(), $page = 1, $page_num = 30, $order_by = 'index_shop_id DESC')
    {
        $data = array(
            'page' => $page,
            'page_num' => $page_num,
            'order_by' => $order_by
        );

        // 支持in查询的字段\支持bettween查询的
        $in_fields = array(
            'id in' => 'index_shop_ids',
            'index_shop_id in' => 'index_shop_ids',
            'brand in' => 'brands',
            'name in' => 'names',
            'index_shop_name in' => 'names',
            'create_at >=' => 'create_at_start',
            'create_at <' => 'create_at_end',
        );

        // 组织条件
        foreach((array)$where as $key => $val) {
            if(in_array($key, array_keys($data))) {
                continue;
            }
            $key = preg_replace('/\s+/', ' ', trim($key));
            if(in_array($key, array_keys($in_fields))) {
                $data[$in_fields[$key]] = $val;
            }
        }

        return $this->post('v1/express/find', $data);
    }

    /**
     * 根据多个网点账号名称批量获取网点
     * @param array $usernames
     * @return array|bool
     */
    public function getShopByNames(array $names)
    {
        if(empty($names) || empty(array_filter($names))) {
            return array();
        }

        return $this->post('v1/shop/find', array('names' => implode(',', $names), 'page_num' => count($names)));
    }

    /**
     * 根据多个网点index_shop_ids批量网点
     * @param array $mobiles
     * @return array|bool
     */
    public function getShopByIds(array $ids)
    {
        if(empty($ids) || empty(array_filter($ids))) {
            return array();
        }
        return $this->post('v1/shop/find', array('index_shop_ids' => implode(',', $ids), 'page_num' => count($ids)));
    }


    /**
     * 逻辑删除一个网点
     * @param $sid
     * @return bool
     */
    public function delShop($sid)
    {
        if(empty($sid)) {
            return false;
        }
        return $this->post('v1/shop/del', array('sid' => $sid));
    }

    /**
     * 编辑网点资料
     * @param $sid
     * @param array $data
     * @return bool
     */
    public function updateShop($sid, $data = array())
    {
        if(empty($sid) || empty($data)) {
            return false;
        }
        return $this->post('v1/shop/update', array('sid' => $sid, 'data' => json_encode($data)));
    }

}